<?php
/**
 * 登录类
 * @author 肖小平
 */
class LoginAction extends Action {
	
	/**
	 * 验证码
	 */
	public function verify(){
		ob_clean();
		import('ORG.Util.Image');
		Image::buildImageVerify();
	}
	/**
	 * 登录
	 */
	public function login(){
		if($_POST){
			if($_SESSION['verify'] != md5($_POST['verify'])) {//判断验证码
				$this->error('验证码错误！');
			}else{
				$data = array();
				$data['account'] = $_POST['account'];
				$data['password'] = md5($_POST['password']);
				$admin = M('Admin')->where($data)->find();
				if ($admin){
					if($admin['state'] == '0'){
						$this->error('用户未激活，请联系管理员！');
					}else{
						//存session
						session('is_login',1);
						session('admin_id',$admin['id']);
						session('account',$admin['account']);
						session('name',$admin['name']);
						header("location:".U('Index/index'));
					}
				}else{
					$this->error('用户名或密码错误！');
				}
			}
		}
		$this->display();
	}
	/**
	 * 修改资料
	 */
	public function update(){
		if($_POST){
			if($_POST['password']){
				$_POST['password'] = md5($_POST['password']);
				M('Admin')->save($_POST);
			}else{
				$data['id'] = $_POST['id'];
				$data['name'] = $_POST['name'];
				M('Admin')->save($data);
			}
			$this->success('修改成功',U('Login/login'));
			exit;
		}
		$id = $_SESSION['admin_id'];
		$obj = M('Admin')->where("id = $id")->find();
		$this->assign('obj',$obj);
		$this->display();
	}
	/**
	 * 登出
	 */
	public function logout(){
		session(null);//清楚session
		header("Location:".U('Login/login'));exit;
	}
	
}